package com.example.java_chatroom.controller;

import com.example.java_chatroom.model.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/friendRequest")
public class FriendRequestController {

    @Resource
    FriendRequestMapper friendRequestMapper;

    @Resource
    FriendMapper friendMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    WebSocketController webSocketController;

    @Resource
    SessionController sessionController;

    /**
     * 获取当前用户的好友申请
     * @param req
     * @return
     */
    @GetMapping("/getFriendRequest")
    public Object getFriendRequest(HttpServletRequest req) {
        List<FriendRequest> friendRequests = new ArrayList<>();
        // 获取当前登录用户的id
        HttpSession session = req.getSession(false);
        if (session == null) {
            System.out.println("[getFriendRequest] 获取session失败！");
            return friendRequests;
        }
        User user = (User)session.getAttribute("user");
        // 根据当前登录用户的userid来查询有没有好友申请
        friendRequests = friendRequestMapper.selectRequestById(user.getUserId());

        // 打印日志
        if (!friendRequests.isEmpty()) {
            System.out.println("当前用户的好友申请：");
            for (FriendRequest request : friendRequests) {
                request.setType("friendRequest");
                System.out.println(request.toString());
            }
        }
        return friendRequests;
    }


    @GetMapping("/handleRequest")
    public Object handleRequest(boolean isAgree, int fromId, String content, HttpServletRequest req) {
        // 获取当前登录用户的id
        HttpSession session = req.getSession(false);
        if (session == null) {
            System.out.println("[handleRequest] 获取session失败！");
            return null;
        }
        User user = (User)session.getAttribute("user");
        // 无论是同意还是拒绝都表示已经处理过这条请求了，处理过的请求要在好友申请表中删除相应的记录
        if (isAgree) {
            // 添加用户到好友列表中
            friendMapper.addFriend(user.getUserId(), fromId);
            // 当前用户同意了对方的好友申请，那么对方用户的好友列表中也要有当前用户
            friendMapper.addFriend(fromId, user.getUserId());

            System.out.println("添加用户："+fromId+"成功！");
            // 在好友请求表中删除这条记录
            friendRequestMapper.deleteFriendRequest(user.getUserId(), fromId);

            String fromName = userMapper.selectUserNameById(fromId);

            // 用户同意申请后将发送申请用户的申请理由显示在消息区域
            // 先创建一个会话(当前登录用户和发送申请消息的用户之间) 返回值是sessionId
            Map<String, Integer> sessionResp = (Map<String, Integer>)
                    sessionController.addSession(fromId, user);
            // 创建会话成功后将申请理由发送在这个会话中
            try {
                webSocketController.sendSystemMessage(sessionResp.get("sessionId"), content,
                        fromId, fromName);
            } catch (IOException e) {
                System.err.println("发送系统消息失败: " + e.getMessage());
            }

            // 构造返回对象
            Friend friend = new Friend();
            friend.setFriendId(fromId);
            friend.setFriendName(fromName);
            return friend;
        } else {
            // 拒绝后的操作
            System.out.println("拒绝用户："+fromId+"的申请");
            friendRequestMapper.deleteFriendRequest(user.getUserId(), fromId);
            return new Friend();
        }
    }


}
